package com.mysterymall.Common.Comp;

import cn.dev33.satoken.stp.StpInterface;
import com.mysterymall.Mapper.RoleMapper;
import com.mysterymall.Mapper.UserMapper;
import com.mysterymall.Model.Entity.Role;
import com.mysterymall.Model.Entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义权限加载接口实现类
 */
@Component  // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserMapper usersMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 返回一个账号所拥有的权限码集合 
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        String id = (String) loginId;

        User user = usersMapper.selectById(id);

        Role role = roleMapper.selectById(user.getRoleId());

        return new ArrayList<String>(Arrays.asList(role.getPowerCode().split("_")));

    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {

        String id = (String) loginId;

        User  user = usersMapper.selectById(id);

        List<String> list = new ArrayList<String>();

        Role  role = roleMapper.selectById(user.getRoleId());

        list.add(role.getName());

        return list;
    }

}
